#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

struct Cargo
{
    char id[20];             //货物号
    int name;                //货物名字
    int mfter;               //生产厂家
    int same_num;            //同类产品的数量
    int price;               //价格
    char producted_date[20]; //生产日期
    char stock_date[20];     //进货日期
    char handle_person[20];  //经手人
};

void cargo_login(Cargo*, int&, int*);  //录入货物信息
void sum_price(Cargo*, int&);          //统计全部货物价格
void num_of_manuf(Cargo*, int&);       //统计某一厂商货物总数
void prices_of_manf(Cargo*, int&);     //统计某一厂商货物总价格
int ReadFromFile(Cargo[]);            //从文件中读取数据
int WriteToFile(Cargo*, int);          //把数据存回文件

int main(int, char**)
{
    Cargo cargos[100];
    int same_cago_num[3] = { 0 };
    int mode;
    int cargo_num = ReadFromFile(cargos);

    printf("********************************************************************************\n"
        "*                         store management system                              *\n"
        "*                                                                              *\n"
        "*                         1.login cargo                                        *\n"
        "*                         2.sumerize the sum price of all cargos               *\n"
        "*                         3.cargo numbers of particular manufacturer           *\n"
        "*                         4.prices of particular manufacturer                  *\n"
        "*                         5.quit                                               *\n"
        "********************************************************************************\n");

    printf("货物名字有:手机、平板、智能手表\n");
    printf("货物厂商有:华为、小米、oppo\n");

    while (1)
    {
        printf("输入你想要选的模式：");
        scanf("%d", &mode);

        if (mode == 5) //mode为5的时候结束程序
        {
            int jude = WriteToFile(cargos, cargo_num);

            if (jude)
            {
                printf("Save sucessfully\n");
            }
            else
            {
                printf("Failed\n");
            }

            printf("The system is shut down!\n");
            break;
        }

        switch (mode)
        {
        case 1:
            cargo_login(cargos, cargo_num, same_cago_num);
            break;
        case 2:
            sum_price(cargos, cargo_num);
            break;
        case 3:
            num_of_manuf(cargos, cargo_num);
            break;
        case 4:
            prices_of_manf(cargos, cargo_num);
            break;
        default:
            break;
        }
    }
}

void cargo_login(Cargo* cargos, int& cargo_num, int* same_cargo_num)
{
    printf("                请输入商品信息                 \n");
    printf("货物名以及商家名以代号的方式录入，具体代号如下\n");
    printf("手机:0     平板:1       智能手表:2\n");
    printf("华为:0     小米:1       oppo:2\n");

    for (int i = 0; i < 100; i++)
    {
        printf("货物号 货物名字 生产厂家 价格 生产日期 进货日期 经手人\n");
        scanf("%s %d %d %d %s %s %s", (cargos + i)->id, &((cargos + i)->name), &((cargos + i)->mfter), &((cargos + i)->price), (cargos + i)->producted_date, (cargos + i)->stock_date, (cargos + i)->handle_person);

        same_cargo_num[(cargos + i)->name]++; //统计同类型货物数量
        (cargos + i)->same_num = same_cargo_num[(cargos + i)->name];
        cargo_num++;
        printf("货物信息录入成功，按1继续录入，其他退出\n");
        int jude;
        scanf("%d", &jude);
        if (jude != 1)
        {
            printf("退出货物录入模式\n");
            break;
        }
    }
}

void sum_price(Cargo* cargos, int& cargo_num)
{
    int sum = 0;
    for (int i = 0; i < cargo_num; i++)
    {
        sum += cargos[i].price;
    }
    printf("所有货物的总价格为: %d\n", sum);
}

void num_of_manuf(Cargo* cargos, int& cargo_num)
{
    int code;
    printf("现在开始查询商家生产产品总数\n");
    printf("请输入想要查询的商家代号(华为:0,小米:1,oppo:2)\n");
    scanf("%d", &code);
    int sum = 0;
    for (int i = 0; i < cargo_num; i++)
    {
        if (cargos[i].mfter == code)
        {
            sum += 1;
        }
    }
    printf("此商家生产产品总数为: %d\n", sum);
}

void prices_of_manf(Cargo* cargos, int& cargo_num)
{
    int code;
    printf("现在开始查询商家生产产品总价格\n");
    printf("请输入想要查询的商家代号(华为:0,小米:1,oppo:2)\n");
    scanf("%d", &code);
    int sum = 0;
    for (int i = 0; i < cargo_num; i++)
    {
        if (cargos[i].mfter == code)
        {
            sum += cargos[i].price;
        }
    }
    printf("此商家生产产品总价格为: %d\n", sum);
}

int ReadFromFile(Cargo * cargos)
{
    FILE* pData = fopen("D:\\mydata.txt", "rb");
    if (pData == NULL)
    {
        printf("Error\n");
        return -1;
    }

    int num = 0;
    while ((fread((void*)&cargos[num], sizeof(Cargo), 1, pData)))
    {
        num++;
    }

    fclose(pData);
    return num;
}

int WriteToFile(Cargo* cargos,int cargo_num)
{
    FILE* pData = fopen("D:\\mydata.txt", "wb");
    if (pData == NULL)
    {
        printf("Error\n");
        return 0;
    }

    for (int i = 0; i < cargo_num; i++)
    {
        fwrite(&cargos[i], sizeof(Cargo), 1, pData);
    }

    fclose(pData);
    return 1;
}